#!/usr/bin/env python
# encoding: utf-8


import os, requests
import asyncio, aiofiles
from aiohttp import ClientSession
from bs4 import BeautifulSoup

header = {
    ':authority:': 'www.3137v.com',
    ':method:': 'GET',
    ':scheme:': 'https',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'cookie': '_ga=GA1.2.1856614864.1515547061; _gid=GA1.2.489910787.1515547061; HstCfa3699098=151554\
    7063175; HstCmu3699098=1515547063175; HstCnv3699098=1; HstCns3699098=1; __dtsu=D9E9B66BBA69555A8\
    03986B802CCD070; HstCla3699098=1515547945614; HstPn3699098=29; HstPt3699098=29',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko)\
     Chrome/63.0.3239.132 Safari/537.36'
}

baseUrl = 'https://www.4084v.com/'


class Operation:
    def __init__(self):
        pass

    async def request_url(self):
        url = baseUrl
        async with ClientSession(headers=header, loop=loop) as session:
            async with session.get(url) as response:
                if response.status == 200:
                    response = await response.read()
                    response = response.decode()
                    soup = BeautifulSoup(response, 'lxml')
                    movie_list = [li for div in soup.find_all('div', class_='box movie_list') for li in
                                  div.find_all('li')]
                    inner_mov_list = [baseUrl + movie.find('a')['href'] for movie in movie_list]
                    for mov in inner_mov_list:
                        await self.request_content(mov)

    async def download_coroutine(self, session, url, title):
        async with session.get(url) as response:
            filename = os.getcwd() + '/data/' + title + '.mp4'
            async with aiofiles.open(filename, 'wb', buffering=1, loop=loop) as fd:
                while True:
                    chunk = await response.content.read(1024)
                    if not chunk:
                        break
                    await fd.write(chunk)

    async def fs(self, title, url):
        r = requests.get(url=url, stream=True)
        filename = os.getcwd() + '/data/' + title + '.mp4'
        async with aiofiles.open(filename, 'wb') as fd:
            for chunk in r.iter_content(1024):
                await fd.write(chunk)

    async def request_content(self, url):
        async with ClientSession(headers=header, loop=loop) as session:
            async with session.get(url) as response:
                if response.status == 200:
                    response = await response.read()
                    response = response.decode()
                    soup = BeautifulSoup(response, 'lxml')
                    download_url = soup.find('div', id='jishu').find('a')['href']
                    title = soup.find('div', class_='film_info clearfix').find('dd', class_='film_title').find(
                        'h1').get_text()
                    # await self.download_coroutine(session, download_url, str(title))
                    await self.fs(str(title), download_url)


def callback(future):
    print('Callback: ', future.result())


if __name__ == '__main__':
    operation = Operation()
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(operation.request_url())
    task.add_done_callback(callback)
    loop.run_until_complete(task)
